package com.bainaeco.framework.util.serializer;

import java.math.RoundingMode;
import java.text.DecimalFormat;

public class Format {
	
	public static final String DEFAULT = "#0.00";
	
	public static final String DECIMAL_NN = "#0.00";
	
	public static final String DECIMAL_NNNN = "#0.0000";
	
	public static final String DECIMAL_N = "#0.0";
	
	public static Double decimal(Double value){
		if(value.isNaN()){
			value = new Double(0);
		}
		DecimalFormat df = new DecimalFormat(DEFAULT);
		df.setRoundingMode(RoundingMode.HALF_UP);
		return Double.valueOf(df.format(value));
	}
	
	public static String decimalToString(Double value){
		if(value.isNaN()){
			value = new Double(0);
		}
		DecimalFormat df = new DecimalFormat(DEFAULT);
		df.setRoundingMode(RoundingMode.HALF_UP);
		return df.format(value);
	}
	
	public static String decimalFour(Double value){
		if(value.isNaN()){
			value = new Double(0);
		}
		DecimalFormat df = new DecimalFormat(DECIMAL_NNNN);
		df.setRoundingMode(RoundingMode.HALF_UP);
		return df.format(value);
	}

	public static Double decimal(Double value,String pattern){
		if(value.isNaN()){
			value = new Double(0);
		}
		DecimalFormat df = new DecimalFormat(pattern);
		df.setRoundingMode(RoundingMode.HALF_UP);
		return Double.valueOf(df.format(value));
	}
	
	public static String decimalToString(Double value,String pattern){
		if(value.isNaN()){
			value = new Double(0);
		}
		DecimalFormat df = new DecimalFormat(pattern);
		df.setRoundingMode(RoundingMode.HALF_UP);
		return df.format(value);
	}
}
